package com.bdqn.controller;

import com.bdqn.entity.bill;
import com.bdqn.entity.provider;
import com.bdqn.entity.user;
import com.bdqn.service.smbms_billService;
import com.bdqn.service.smbms_providerService;
import com.bdqn.util.page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/bill")
public class billcontroller {
    @Autowired
    private smbms_billService bill;
    @Autowired
    private smbms_providerService pro;

    @RequestMapping("/getAllBill")
    public String getAllBill(@RequestParam(required = false) String queryProductName ,
                             @RequestParam(required = false) Integer queryProviderId,
                             @RequestParam(required = false) Integer queryIsPayment,
                             @RequestParam(required = false) Integer currentPageNo, Model model, HttpServletRequest request){
        if (currentPageNo == null){
            currentPageNo = 1;
        }
        if (queryProviderId != null && queryProviderId != 0 ){
            model.addAttribute("queryProviderId",queryProviderId);
        }else {
            queryProviderId = null;
        }
        if (queryIsPayment != null && queryIsPayment != 0 ){
            model.addAttribute("queryIsPayment",queryIsPayment);
        }else {
            queryIsPayment = null;
        }


         Map map = new HashMap<>();
         map.put("id",queryProviderId);
         map.put("ductname",queryProductName);
         map.put("isPayment",queryIsPayment);
         map.put("currentPageNo",(currentPageNo-1)*10);
         map.put("pageSize",10);


        page pa = new page();
        pa.setPageSize(10);
        pa.setTotalCount(bill.getAllCount(map));
        pa.setCurrentPageNo(currentPageNo);

        List<bill> billList = bill.getbillByparam(map);
        List<provider> providerList = pro.providerList();
        request.getSession().setAttribute("billList",billList);
        request.getSession().setAttribute("providerList",providerList);
        request.getSession().setAttribute("page",pa);
        return "jsp/billlist";
    }

    @RequestMapping("/{id}")
    public String getUser(@PathVariable(value = "id") Integer id, HttpServletRequest request, String opr){
        request.getSession().setAttribute("bill",bill.fingBillByid(id));
        request.getSession().setAttribute("proList", pro.providerList());
        return "redirect:/jsp/"+opr+".jsp";
    }

    @RequestMapping("/addBill")
     public String addBill(bill bi,HttpServletRequest request){
            bi.setCreationDate(new Date());
            user u = (user) request.getSession().getAttribute("user");
            bi.setCreatedBy(u.getId());
            bi.setProductDesc("不详");
            int i = bill.addBill(bi);
            if (i>0){
                System.out.println("添加成功！！！");
            }else {
                return "jsp/billadd";

            }
           return "redirect:getAllBill";
    }

    @RequestMapping("/getAllProvider")
    @ResponseBody
    public Object getProvider(bill bi,HttpServletRequest request){
        List<provider> providerList = pro.providerList();
        return  providerList;
    }

    @RequestMapping("/updBill")
    public String updBill(bill bi,HttpServletRequest request){
        bi.setCreationDate(new Date());
        user u = (user) request.getSession().getAttribute("user");
        bi.setModifyBy(u.getId());
        bi.setModifyDate(new Date());
        int i = bill.updBill(bi);
        if (i>0){
            System.out.println("修改成功！！！");
        }else {
            return "jsp/billmodify";

        }
        return "redirect:getAllBill";
    }

    @RequestMapping("/delBill")
    @ResponseBody
    public Object delBill(Integer id){
        int num = bill.delbill(id);
        if (num>0){
            return  "true";
        }else {
            return  "false";

        }
    }

}
